package java_io.csv;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class ReadCSV {
	
	public static void main(String[] args) {
		
		ArrayList<Contact> contacts = new ArrayList<Contact>();
		contacts = readFile();
		for (Contact c: contacts){
			c.print();
		}

	}
	
	public static ArrayList<Contact> readFile(){
		ArrayList<Contact> contacts = new ArrayList<Contact>();
		final String dir = System.getProperty("user.dir");
		Charset charset = Charset.forName("US-ASCII");
		Path path = Paths.get(dir + "\\bin\\javacore\\io\\contacts.csv");
		
		try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
		    String line = null;
		    Contact c;
		    
		    while ((line = reader.readLine()) != null) {
		    	String[] s = line.split(",");
		    	c = new Contact(s[0],s[1],s[2],s[3]);
		    	contacts.add(c);
		        //System.out.println(line);
		    }
		} catch (IOException x) {
		    System.err.format("IOException: %s%n", x);
		}
		
		return contacts;
	}

}
